package com.wiscamp.ninechapters.problems.math;

import com.wiscamp.ninechapters.common.core.ParseHelper;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;

/**
 * 科学计数法
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class LatexScienceNotation extends LatexNumber {
    /**
     * 十的次方
     */
    private int exponentiation = 0;

    /**
     * 基数
     */
    private BigDecimal baseNumber;

    /**
     * 正负号
     */
    private String symbol = "";


    public String getLatex() {
        if (symbol.equals("+"))
            symbol = "";
        var exponentiationSymbol = "";
        if (exponentiation > 0)
            exponentiationSymbol = "+";

        return String.format("%s%sE%s", symbol, baseNumber.stripTrailingZeros().toPlainString(), exponentiationSymbol + exponentiation);
    }

    public LatexScienceNotation(String symbol, BigDecimal baseNumber, int exponentiation) {
        this.symbol = symbol;
        this.baseNumber = baseNumber;
        this.exponentiation = exponentiation;
    }

    public LatexScienceNotation(String content) {
        var text = content.toUpperCase();
        var array = text.split("E");
        if (array.length == 2) {
            this.baseNumber = ParseHelper.parseBigDecimal(array[0]);
            this.exponentiation = ParseHelper.parseInteger(array[1]);
            if (this.baseNumber.compareTo(BigDecimal.valueOf(0L)) < 0) {
                this.symbol = "-";
            }
        }
    }
}
